package com.addcn.pushlibrary.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import androidx.room.util.StringUtil;

import java.io.*;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * author:luwenbo
 * created on date : 2019-05-14
 * content:
 * describe:
 */
public class LBuildProperties {

    private static Properties properties = null;
    private static String ROM_TAG = "LBuildProperties";
    //public static String SYS_TEP
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
    private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
    private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
    private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";

    private static final String KEY_OPPO_VERSION_NAME = "ro.build.version.opporom";
    private static final String KEY_VIVO_VERSION_NAME = "ro.vivo.os.version";

    /**
     * 读取用户设备的配置信息
     */
    public static void loadProperties(Context context) {
        try {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
                if (!TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_CODE, ""))
                        || !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_NAME, ""))
                        || !TextUtils.isEmpty(getSystemProperty(KEY_MIUI_INTERNAL_STORAGE, ""))) {
                    LRomUtil.userType = LRomUtil.ROM_TYPE.Mi;//小米
                } else if (!TextUtils.isEmpty(getSystemProperty(KEY_EMUI_API_LEVEL, ""))
                        || !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_VERSION, ""))
                        || !TextUtils.isEmpty(getSystemProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, ""))) {
                    LRomUtil.userType = LRomUtil.ROM_TYPE.Huawei;//华为
                } else if (!TextUtils.isEmpty(getSystemProperty(KEY_OPPO_VERSION_NAME, ""))) {
                    LRomUtil.userType = LRomUtil.ROM_TYPE.OPPO;//OPPO
                } else if (!TextUtils.isEmpty(getSystemProperty(KEY_VIVO_VERSION_NAME, ""))) {
                    LRomUtil.userType = LRomUtil.ROM_TYPE.VIVO;//OPPO
                } else {
                    if (LRomUtil.isFcm(context)) {
                        LRomUtil.userType = LRomUtil.ROM_TYPE.FCM;
                    } else {
                        LRomUtil.userType = LRomUtil.ROM_TYPE.JPush;
                    }
                }
            } else {
                properties = new Properties();
                Process p = Runtime.getRuntime().exec("getprop");
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = in.readLine()) != null && !line.equals("null")) {
                    parseLine(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(ROM_TAG, "load build.prop fail");
        }

        if (properties == null) {
            Log.e(ROM_TAG, "loadProperties, properties is null");
        } else {
            Log.i(ROM_TAG, "loadProperties, properties init ok");
        }
    }

    private static final Pattern regex = Pattern.compile("\\[(.+)]: \\[(.+)]");

    private static void parseLine(String line) {
        if (line != null && !line.equals("")) {
            Matcher m = regex.matcher(line);
            if (m.find()) {
                properties.setProperty(m.group(1), m.group(2));
            }
        }
    }


    /**
     * 判断是否包含特定配置项，以区分不同的手机类型
     *
     * @param key 配置项字符串
     * @return true-包含
     */
    public static boolean containsKey(final Object key) {
        if (properties != null) {
            return properties.containsKey(key);
        }
        return false;
    }

    public static boolean containsValue(final Object value) {
        return properties.containsValue(value);
    }

    public static String getProperty(final String name) {
        return properties.getProperty(name);
    }

    private static String getSystemProperty(String key, String defaultValue) {
        try {
            Class<?> clz = Class.forName("android.os.SystemProperties");
            Method get = clz.getMethod("get", String.class, String.class);
            return (String) get.invoke(clz, key, defaultValue);
        } catch (Exception e) {
        }
        return defaultValue;
    }
}
